package uniandes.dermatologia;

import java.util.Calendar;
import java.util.Date;

import org.joda.time.DateTime;
import org.joda.time.Days;
import org.joda.time.Years;

import uniandes.dermatologia.models.Anamnesis.Age;
import uniandes.dermatologia.models.Anamnesis.Gender;
import uniandes.dermatologia.singleton.DermatologiaUniandesApp;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.RadioGroup;

public class IdentificationActivity extends Activity {

	private RadioGroup groupGender;
	private DatePicker birthDate;	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_identification);
		
		getActionBar().setDisplayHomeAsUpEnabled(false);

		groupGender = (RadioGroup) findViewById(R.id.radioGroupIdentification);
		birthDate = (DatePicker) findViewById(R.id.datePickerBirth);

		Button btnBack = (Button) findViewById(R.id.btnBackIdentification);
		Button btnNext = (Button) findViewById(R.id.btnNextIdentification);

		Typeface font = Typeface.createFromAsset(getAssets(),
				"fonts/Roboto-Light.ttf");

		btnBack.setTypeface(font);
		btnNext.setTypeface(font);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.identification, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void onClickNext(View v) {

		Calendar fecha = Calendar.getInstance();
		fecha.set(birthDate.getYear(), birthDate.getMonth(),
				birthDate.getDayOfMonth());

		Date date = fecha.getTime();

		int anios = Years.yearsBetween(new DateTime(date),
				new DateTime(new Date())).getYears();
		int dias = Days.daysBetween(new DateTime(date),
				new DateTime(new Date())).getDays();

		if (dias <= 28) {
			((DermatologiaUniandesApp) getApplication())
					.getCurrentConsultation().getAnamnesis()
					.setAge(Age.neonate);
		} else {
			if (anios >= 0 && anios < 1) {
				((DermatologiaUniandesApp) getApplication())
						.getCurrentConsultation().getAnamnesis()
						.setAge(Age.infant);
			}
			if (anios >= 1 && anios <= 4) {

			} else if (anios >= 5 && anios < 10) {
				((DermatologiaUniandesApp) getApplication())
				.getCurrentConsultation().getAnamnesis()
				.setAge(Age.child);
			} else if (anios >= 10 && anios < 14) {
				((DermatologiaUniandesApp) getApplication())
				.getCurrentConsultation().getAnamnesis()
				.setAge(Age.child);
			} else if (anios >= 14 && anios < 21) {
				((DermatologiaUniandesApp) getApplication())
				.getCurrentConsultation().getAnamnesis()
				.setAge(Age.child);
			} else if (anios >= 21 && anios < 55) {
				((DermatologiaUniandesApp) getApplication())
				.getCurrentConsultation().getAnamnesis()
				.setAge(Age.adult);
			} else if (anios >= 55 && anios < 70) {

			} else if (anios > 70) {

			}
		}

		((DermatologiaUniandesApp) getApplication()).getCurrentConsultation()
				.getAnamnesis().setBirthDate(fecha.getTime());

		if (groupGender.getCheckedRadioButtonId() == R.id.radioMale)
			((DermatologiaUniandesApp) getApplication())
					.getCurrentConsultation().getAnamnesis()
					.setGender(Gender.men);

		else if (groupGender.getCheckedRadioButtonId() == R.id.radioFemale) {
			((DermatologiaUniandesApp) getApplication())
					.getCurrentConsultation().getAnamnesis()
					.setGender(Gender.women);

		}

		Intent intent = new Intent(this, PhototypeActivity.class);
		startActivity(intent);

	}

	public void onClickBack(View v) {
		
		finish();
	}	
	
}
